#region (c) 2014 THN Solutions LLC. - All rights reserved
/*
THN TELEPORT version 2

Copyright (c) 2014, THN Solutions LLC. ( www.ThnSolutions.com )
Author: Nguyễn, M. Hải                 ( www.Minh-Hai.com     )
All rights reserved.

This library is dual-licensed.
  + For commercial software, please obtain a commercial license from THN Solutions LLC.

  + For free software, this library is licensed under GPL version 3. A summary of GPLv3 is
    listed below. You should also find a copy of GPLv3 as file License_GPLv3.txt included
	with the source-code files.

    This program is free software: you can redistribute it and/or modify
    it under the terms of the GNU General Public License version 3 as published by
    the Free Software Foundation  of the License.

    This program is distributed in the hope that it will be useful,
    but WITHOUT ANY WARRANTY; without even the implied warranty of
    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
    GNU General Public License for more details.

    You should have received a copy of the GNU General Public License
    along with this program.  If not, see <http://www.gnu.org/licenses/>.
*/
#endregion

#region Using directives
using System;
using Thn;
using Thn.Serialization;
#endregion

namespace Thn.Communication.Teleporting
{
	/// <summary>
	/// This codec uses <see cref="XmlSerializer"/> format for serialization. Params are required to have [Savable] attributes. Non-public members included.
	/// </summary>
	internal class XmlCodec: ITextCodec
	{
        #region Fields
        XmlSerializer mSerializer;
        #endregion

		#region ICodec Members
		/// <summary>
		/// Serialize object
		/// </summary>
		public string Encode(object value)
		{            
            return mSerializer.Serialize(value).AsString;
		}

		/// <summary>
		/// Serialize parameters
		/// </summary>
		public string Encode(params object[] values)
		{
            return mSerializer.Serialize(values).AsString;
		}

		/// <summary>
		/// Deserialize parameters
		/// </summary>
		public object Decode(string encodedArgs)
		{
            return mSerializer.Deserialize(encodedArgs);
		}

		#endregion

		#region Constructor
		/// <summary>
		/// Default constructor
		/// </summary>
		public XmlCodec()
		{
			XmlSerializer serializer = new XmlSerializer();
            serializer.RequiredAttributes = new Attribute[] { new SavableAttribute() };            
            serializer.IncludeFields = true;
            serializer.IncludeProperties = true;                  
			mSerializer = serializer;
		}
		#endregion
	}
}
